<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 用户
 * @author www.dingwei.cn
 */
class News extends MY_Controller {
	
	/**
	 * 新闻模型
	 * @var AdmNewsModel
	 */
	public $mNews;
	/**
	 * 新闻分类模型
	 * @var AdmNewsTypeModel
	 */
	public $mNewsType;
	public function __construct(){
		parent::__construct();
		$this->loadModel('AdmNewsTypeModel', 'mNewsType','admin');
		$this->loadModel('AdmNewsModel', 'mNews','admin');
	}
	
	/**
	 * 获取新闻列表
	 */
	public function getNewList(){
		//表单验证
		$vConf = array(
// 				array('type_id', '类型id', 'is_natural_no_zero'),//
				array('page', '分页', 'required|is_natural_no_zero'),//
// 				array('keyword', '关键字', ''),
		);
		$this->formDataValidation($vConf);
		
		$typeId = trim(post('type_id'));
		$page = trim(post('page'));
		$keyword = trim(post('keyword'));//关键字
		
		//获取分类
		if ($typeId){
			if ( ! $this->mNewsType->getInfo($typeId)){
				$this->appReturn(0,'分类不存在');
			}
		}
		
		$tA['list'] = $this->mNews->appGetList($typeId,0,$keyword,$page);
		$tA['top'] = array();
		if ( ! $keyword){
			$tA['top'] = $this->mNews->getTopList($typeId);
		}
		$this->appReturn(1,'',$tA);
	}
	
	/**
	 * 获取分类列表
	 */
	public function getTypeList(){
		$tA = $this->mNewsType->getTypeList();
		array_unshift($tA,array(
				'id'=>'0',
				'type_name'=>'全部',
				'sort'=>"0",
				'addtime'=>"0"
				
		));
		$this->appReturn(1,'',$tA);
	}
	
	/**
	 * 首页接口
	 */
	public function index(){
		
		
	}
	
	/**
	 * 获取详情
	 */
	public function getInfo(){
		//表单验证
		$vConf = array(
				array('news_id', '新闻id', 'required|is_natural_no_zero'),//
		);
		$this->formDataValidation($vConf);
		
		$newsId = trim(post('news_id'));
		
		$tA = $this->mNews->getInfo($newsId);
		if ( ! $tA){
			$this->appReturn(0,'没有数据');
		}
		
		/**
		 * 是否收藏
		 */
		$tA['is_collection'] = "1";//没有收藏
		if ($this->mUser->hasCollection($this->userId, $newsId, 3)){
			$tA['is_collection'] = "2";//有收藏
		}

		//浏览次数
		if ($this->mNews->newsView($this->userId, $newsId)){
			if ( ! $this->mNews->save(array('read_number'=>$tA['read_number'] + 1),$newsId)){
				$this->appReturn(0,'浏览失败');
			}
		}
// 		print_r($tA);
		$html = $this->view('news/info',['html'=>$tA],0,0,0,true);

		$data = array(
				'is_collection'=>$tA['is_collection'],
				'html'=>$html,
		);
		$this->appReturn(1,'',$data);
	}

    /**
     * 获取详情
     */
    public function getInfoForShare(){


        $newsId = (int)get('id');

        $tA = $this->mNews->getInfo($newsId);
        if ( ! $tA){
            show_404();
        }

        $this->view('news/info', array('html' => $tA));
    }
}